<template>
    <view class="uc-wrap uc-home-home">
    	<ucHead :title="userObj.name"></ucHead>

        <view class="box">
            
            <view class="g-card">
                <view class="img"><image :src="userObj.avatarUrl" mode="aspectFill"></image></view>
                <view class="hd">
					<view class="be" v-if="userObj.companyLogo"><image :src="userObj.companyLogo" mode="aspectFill"></image></view>
                    {{userObj.companyName}}
                </view>
                <view class="name">
                    <view class="n">{{userObj.name}}</view>
                    <view class="t">{{userObj.jobName}}</view>
                </view>
                <view class="bd">
                    <view class="item">
                        <div class="con">{{userObj.phone}}</div>
                        <image src="@/static/images/card-call.png" mode="widthFix" class="uc-icon24"></image>
                    </view>
                    <view class="item">
                        <div class="con">{{userObj.email}}</div>
                        <image src="@/static/images/card-mail.png" mode="widthFix" class="uc-icon24"></image>
                    </view>
                    <view class="item">
                        <div class="con" >{{userObj.address}}</div>
                        <image src="@/static/images/card-addr.png" mode="widthFix" class="uc-icon24"></image>
                    </view>
                </view>
            </view>

            <view class="top-operate">
                <button class="uc-btn green-plain" hover-class="none" @click="openShare">分享名片</button>
                <button class="uc-btn green" hover-class="none" @click="getPhoneNumber" type="primary">存入手机通讯录</button>
            </view>

            <view class="scroll-box">
                <scroll-view scroll-x="true" class="scrollViewX" scroll-with-animation="true">
                    <view class="item" @click="clickTel">
                        <view class="hd">
                            <image src="@/static/images/home-icon-2-1.png" mode="widthFix" class="uc-icon40"></image>拨打电话
                        </view>
                        <view class="bd" style="overflow: hidden;">{{userObj.phone}}</view>
                    </view>
                    <view class="item" @click="clickCopy(userObj.wechart)">
                        <view class="hd">
                            <image src="@/static/images/home-icon-2-2.png" mode="widthFix" class="uc-icon40"></image>加我微信
                        </view>
                        <view class="bd"style="overflow: hidden;">{{userObj.wechart}}</view>
                    </view>
                    <view class="item" @click="clickCopy(userObj.email)">
                        <view class="hd">
                            <image src="@/static/images/home-icon-2-3.png" mode="widthFix" class="uc-icon40"></image>邮箱
                        </view>
                        <view class="bd"style="overflow: hidden;">{{userObj.email}}</view>
					</view>
                </scroll-view>
            </view>
        </view>
        <view class="title">
            <image src="@/static/images/home-title-2.png" mode="widthFix" class="uc-icon36"></image>
            我的企业
        </view>

        <view class="box2">
            <view class="img"><image :src="userObj.companyLogo" mode="aspectFill"></image></view>
            <view class="c">{{userObj.companyName}}</view>
            <navigator class="uc-btn green-plain" url="" hover-class="none" @click="clickGW">进入官网</navigator>
        </view>
        <view class="title">
            <image src="@/static/images/home-title-3.png" mode="widthFix" class="uc-icon36"></image>
            我的视频
        </view>
        <view class="video-box">
            <video id="myVideo" v-if="userObj.videoUrl" :src="userObj.videoUrl" @error="videoErrorCallback" controls></video>
        </view>
        <view class="title">
            <image src="@/static/images/home-title-4.png" mode="widthFix" class="uc-icon36"></image>
            最新动态
            <navigator class="more" url="" hover-class="none" @click="clickDT">更多动态 <text class="uc-font uc-arrow-right"></text></navigator>
        </view>

        <view class="g-hotelItem" v-for="(item,index) in feedObjList" :key="index">
            <view class="shop">
                <view class="face"><image src="https://my.bziyun.com/20250619/face_1750325367141.png" mode="aspectFill"></image></view>
                <view class="con">
                    <view class="name">{{userObj.companyName}}</view>
                    <view class="time">{{item.createTime}}</view>
                </view>
            </view>
            <view class="tags">
                <text class="tag">#{{item.feedTypeName}}</text>
            </view>
            <view class="desc">{{item.feedName}}</view>
            <view class="imgs-box">
                <view class="img" v-for="(itemImg,indexImg) in item.feedPicList" :key="indexImg" @click="btnShowHotelItemImgs(item.feedPicList,indexImg)">
                    <image :src="itemImg" mode="aspectFill"></image>
                </view>
            </view>
            <view class="btm">
                <view class="l">
                    <view class="face"><image src="https://my.bziyun.com/20250619/face-default_1750324928973.png" mode="aspectFill"></image></view>
                    1人点赞
                </view>
                <view class="btn" @click="isFinger = !isFinger">
                    <image src="@/static/images/hotelItem-icon-2-on.png" mode="widthFix" class="uc-icon36" v-if="isFinger"></image>
                    <image src="@/static/images/hotelItem-icon-2.png" mode="widthFix" class="uc-icon36" v-else></image>
                    1
                </view>
            </view>
        </view>

        <view class="operate">
            <navigator class="btn" url="" hover-class="none" @click="clickDT">
                更多动态 <text class="uc-font uc-arrow-right"></text>
            </navigator>
        </view>
        <view class="title">
            <image src="@/static/images/home-title-5.png" mode="widthFix" class="uc-icon36"></image>
            我的照片
        </view>
        <view class="list5"  v-if="userObj.picList&&userObj.picList.length>0">
            <view class="item" v-for="(item,index) in userObj.picList" :key="index">
                <image :src="item" mode="widthFix"></image>
            </view>
        </view>
    </view>
    <ucFoot :selectIndex="0"></ucFoot>
	
	<view
			class="uc-alert uc-alert-Share"
			v-if="showShare"
			@touchmove.stop.prevent="doNothing"
		  >
	  <view class="over-close" @click="showShare = false"></view>
	  <view class="box-bottom">
	    <view class="part">
	      <button class="btn" plain open-type='share' style="border: none;">
	        <image
	          src="https://my.bziyun.com/20250619/alert-Share-icon-1_1750323144123.png"
	          mode="widthFix"
	          class="uc-icon120"
	        ></image
	        >微信好友
	      </button>
	      <view class="btn" @click="openHaibao">
	        <image
	          src="https://my.bziyun.com/20250619/alert-Share-icon-2_1750323223360.png"
	          mode="widthFix"
	          class="uc-icon120"
	        ></image
	        >保存海报
	      </view>
	    </view>
	    <view class="operate">
	      <button class="uc-btn" hover-class="none" @click="showShare = false">取消</button>
	    </view>
	  </view>
	</view>
	
</template>
<script setup>
    import { ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { useMainStore } from '@/store/index.js'
	const mainStore= useMainStore();
	const showShare = ref(false)
	const openShare = ()=>{
		showShare.value = true
	}
	const clickShare = ()=> {
		console.log("dianjil");
	      uni.share({
	        provider: 'weixin',  // 分享平台（例如微信）
	        type: 0,  // 分享类型（0: 分享文本 1: 分享图片 2: 分享链接）
	        title: '自定义分享标题',
	        imageUrl: 'http://example.com/share-image.png',
	        url: 'https://www.example.com',  // 分享的链接
	        success: function (res) {
	          uni.showToast({
	            title: '分享成功',
	            icon: 'success'
	          });
	        },
	        fail: function (err) {
	          uni.showToast({
	            title: '分享失败',
	            icon: 'error'
	          });
	        }
	      });
	}
	const clickGW = ()=>{
		uni.navigateTo({
		    url: `/pages/site/site` // 注意路径写法，从pages目录开始写起，且文件名首字母大写，后缀为.vue的文件名部分。
		});
	}
	const clickDT = ()=>{
		uni.navigateTo({
		    url: `/pages/dynamics/dynamics` // 注意路径写法，从pages目录开始写起，且文件名首字母大写，后缀为.vue的文件名部分。
		});
	}
	const clickTel = ()=>{
		uni.makePhoneCall({
			phoneNumber: userObj.value.phone,
			success: () => {
				console.log('呼叫成功');
			},
			fail: () => {
				console.log('呼叫失败');
			}
		});
	}
	
	const openHaibao = ()=>{
		console.log("跳转到海报页");
		uni.navigateTo({
		    url: `/pages/user/share` // 注意路径写法，从pages目录开始写起，且文件名首字母大写，后缀为.vue的文件名部分。
		});
	}
	
	const clickCopy = (text)=>{
		uni.setClipboardData({
			data: text,
			success: function () {
			  uni.showToast({
				title: '复制成功',
				icon: 'success',
				duration: 2000
			  });
			}
		});
	}
	
	const getPhoneNumber = ()=>{
		uni.addPhoneContact({
		    firstName: userObj.value.name,
		    mobilePhoneNumber: userObj.value.phone,
		    email: userObj.value.email,
		    organization: userObj.value.companyName,
		    success: () => {
		        uni.showToast({
		            title: '已添加到通讯录',
		            icon: 'success'
		        });
		    },
		    fail: () => {
		        uni.showToast({
		            title: '添加联系人失败',
		            icon: 'none'
		        });
		    }
		});
	}
	
	
	const nameObj = ref('')
	onLoad((options) => {	
	  // 情况1：scene 字符串存在（扫码进入）
	  if (options.scene) {
	      const decoded = decodeURIComponent(options.scene)
	      const dataList = decoded.split("=")
		  nameObj.value = dataList.length>1?dataList[1]:''
		  mainStore.setUserCode(nameObj.value)
		  wx.setStorage({
		    key: "code",
		    data: nameObj.value
		  });
		  clickitem()
		  localStorage.setItem("code",nameObj.value)
	    }else if (options.id) {
			console.log('页面参数 id:', options.id)
			wx.setStorage({
			  key: "code",
			  data: options.id
			});
			clickitem()
		}else{
			wx.getStorage({
			  key: "code",
			  success: function(res) {
				mainStore.setUserCode(res.data)
			  }
			});
		}

	})
	setTimeout(()=>{
		const code = uni.getStorageSync('code')
		if(code){
			mainStore.setUserCode(code)
		}
		clickitem()
		// clickitem("7548432b")
		// mainStore.setUserCode('7548432b')
		// wx.setStorage({
		//   key: "code",
		//   data: "7548432b"
		// });
		
	},500)
	const uniToken =ref({})
	const newCode = ref('')
	//获取信息
	const clickitem = ()=>{
		uni.login({
		  provider: 'weixin',  
		  success: (res) => {
		    // 调用后端接口，传递 res.code 获取用户的 openid 等信息
			uni.request({
			  url: `https://muyin-pc.51wephone.cn/app-api/front/user/login?jsCode=${res.code}`, 
			  method: 'GET',
			  data: {},
			  success: async (response) => {
				console.log('后端返回：', response.data);
				uniToken.value = response.data.data
				mainStore.setUserInfo(response.data.data)
				//用户信息
				await getUserInfo()
			  },
			  fail: (err) => {
				console.error('请求后端失败：', err);
			  },
			});
		  },
		  fail: (err) => {
		    console.log('登录失败', err)
		  }
		})
	}
	//用户信息
	const userObj = ref(null)
	const getUserInfo = ()=>{
		console.log(mainStore.userInfo.token,"mainStore.userInfo");
		uni.request({
			url: `https://muyin-pc.51wephone.cn/app-api/front/card/getCardInfoWithCode/${mainStore.userCode}`,
			method: 'GET', 
			data: { 
			},
			header:{
				"TOKEN":mainStore.userInfo.token,
			},
			success: (res) => {
				console.log('请求成功:', res.data);
				userObj.value = res.data.data
				mainStore.setUserInfoObj(res.data.data)
				//公司动态
				getfeedInfo()
			},
			fail: (err) => {
				console.error('请求失败:', err);
			}
		});
	}
	// 动态
	const feedObjList = ref(null)
	const getfeedInfo = ()=>{
		uni.request({
			url: `https://muyin-pc.51wephone.cn/app-api/feed/queryList`,
			method: 'POST', 
			data: { 
				pageNum:1,
				pageSize:5,
			},
			header:{
				"TOKEN":mainStore.userInfo.token,
			},
			success: (res) => {
				console.log('请求成功:', res.data);
				feedObjList.value = res.data.data
			},
			fail: (err) => {
				console.error('请求失败:', err);
			}
		});
	}
    const videoErrorCallback = e =>{
        // uni.showModal({
        //     content: e.target.errMsg,
        //     showCancel: false
        // })
    }
    const isFinger = ref(false)
    const btnShowHotelItemImgs = (urls,index) =>{
        uni.previewImage({
            current:index,
            urls: urls,
            longPressActions: {
                itemList: ['发送给朋友', '保存图片', '收藏'],
                success: function(data) {
                    console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
                },
                fail: function(err) {
                    console.log(err.errMsg);
                }
            }
        });
    }

	// 分享至微信
	const onShareAppMessage = ()=>{
		 //   uni.share({
			// title: '分享标题测试1', // 分享标题
			// path: `/pages/home/home?id=7548432b`, // 分享路径，填写当前页面路径和参数
			// imageUrl: 'https://my.bziyun.com/20250624/card-person_1750323330263_1750728491020.png', //5:4图片
			// success: function(res) {
			//   // 转发成功之后的回调函数，可以在这里进行一些统计或者操作
			//   console.log('转发成功', res);
			// },
			// fail: function(res) {
			//   // 转发失败之后的回调函数，可以在这里进行一些错误处理或者提示用户
			//   console.log('转发失败', res);
			// }
		 //  });
		  return {
			title: '分享标题测试1', // 分享标题
			path: `/pages/home/home?id=${mainStore.userCode}`, // 分享路径，填写当前页面路径和参数
			imageUrl: 'https://my.bziyun.com/20250624/card-person_1750323330263_1750728491020.png', //5:4图片
		  }
	}
</script>
